﻿using System;
using System.Web.Services;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Web.Common;

namespace QuanLyHopDong.Web.DanhMuc.DonVi
{
    public class List : System.Web.UI.Page
    {
        #region Properites

        private static DonViBiz _donViBiz;
        private static DonViBiz DonViManager
        {
            get { return _donViBiz ?? (_donViBiz = new DonViBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (WebCommon.CurrrentEmployee == null)
            //{
            //    Response.Redirect("~/Logon.aspx");
            //    return;
            //}

            if (!WebCommon.CurrrentEmployee.IsAdmin)
            {
                Response.Redirect("~/NoPermission.aspx");
            }
        }

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object DonViList(int jtStartIndex, int jtPageSize, string jtSorting, string name)
        {
            int totalRecords;
            var donVis = DonViManager.GetDonVis(jtStartIndex, jtPageSize, jtSorting, name, out totalRecords);

            return new { Result = "OK", Records = donVis, TotalRecordCount = totalRecords };
        }

        [WebMethod(EnableSession = true)]
        public static object Delete(int Id)
        {

            DonViManager.DeleteDonVi(Id);
            return new { Result = "OK" };
        }

        [WebMethod(EnableSession = true)]
        public static object Create(Biz.Model.DonVi record)
        {
            try
            {
                DonViManager.CreateDonVi(record);
                return new { Result = "OK", Record = record };
            }
            catch (ApplicationException appEx)
            {
                return new { Result = "ERROR", Message = string.Format(WebCommon.GetGlobalString(appEx.Message), record.Ten) };
            }
        }

        [WebMethod(EnableSession = true)]
        public static object Update(Biz.Model.DonVi record)
        {
            try
            {
                DonViManager.UpdateDonVi(record);
                return new { Result = "OK" };
            }
            catch (ApplicationException appEx)
            {
                return new { Result = "ERROR", Message = string.Format(WebCommon.GetGlobalString(appEx.Message), record.Ten) };
            }
        }

        #endregion
    }
}